package com.baidu.bos.web.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class ProcessInstanceAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	private String id;
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * 查看所有正在运行的流程实例
	 * @return
	 */
	public String list(){
		ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
		List<ProcessInstance> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	/**
	 * Ajax查询流程变量
	 * @throws IOException 
	 */
	public String findData() throws IOException{
		Map<String, Object> variables = processEngine.getRuntimeService().getVariables(id);
		ServletActionContext.getResponse().setContentType("html/text;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(variables);
		return NONE;
	}
	
	//查看流程图
	public String showPng(){
		//根据流程实例id查询流程实例对象
		ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
		query.processInstanceId(id);
		ProcessInstance processInstance = query.singleResult();
		//根据流程实例查询对应流程定义id
		String processDefinitionId = processInstance.getProcessDefinitionId();
		//根据流程定义id查询流程定义对象
		ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) processEngine.getRepositoryService().getProcessDefinition(processDefinitionId);
		//根据流程定义对象获得部署id和png图片名称
		String deploymentId = processDefinition.getDeploymentId();
		String diagramResourceName = processDefinition.getDiagramResourceName();
		//根据流程实例对象获取当前运行到哪个节点
		String activityId = processInstance.getActivityId();
		ActivityImpl activityImpl = processDefinition.findActivity(activityId);
		//获取坐标
		int x = activityImpl.getX();
		int y = activityImpl.getY();
		int width = activityImpl.getWidth();
		int height = activityImpl.getHeight();
		ActionContext.getContext().getValueStack().set("deploymentId", deploymentId);
		ActionContext.getContext().getValueStack().set("diagramResourceName", diagramResourceName);
		ActionContext.getContext().getValueStack().set("x", x);
		ActionContext.getContext().getValueStack().set("y", y);
		ActionContext.getContext().getValueStack().set("width", width);
		ActionContext.getContext().getValueStack().set("height", height);
		return "showPng";
	}
	private String deploymentId;
	private String imageName;
	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}
	public String viewImage(){
		InputStream pngStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, imageName);
		ActionContext.getContext().getValueStack().set("pngStream", pngStream);
		return "viewImage";
	}
}
